package vtt.android.dapanhc.activities;

import vtt.android.dapanhc.Constants;
import vtt.android.dapanhc.MyBroadcastReceiver;
import vtt.android.dapanhc.R;
import vtt.android.dapanhc.utils.VttAlarmManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SettingActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		final SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARE_PREFERENCE_NAME, MODE_PRIVATE);
		final Editor sharedEdit = sharedPreferences.edit();
		
		final View seekContainer = findViewById(R.id.seekbar_container);
		
		CheckBox cbox = (CheckBox) findViewById(R.id.checkbox_noti);
		boolean isShowNotice = sharedPreferences.getBoolean(Constants.SHARE_KEY_SHOW_NOTICE, true);
		cbox.setChecked(isShowNotice);
		
		// hide seekbar if not checked
		if(!isShowNotice){
			seekContainer.setVisibility(View.GONE);
		}
		
		cbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					seekContainer.setVisibility(View.VISIBLE);
				} else {
					seekContainer.setVisibility(View.GONE);
				}
				sharedEdit.putBoolean(Constants.SHARE_KEY_SHOW_NOTICE, isChecked);
				sharedEdit.commit();
			}
		});
		
		int hour = sharedPreferences.getInt(Constants.SHARE_KEY_NOTICE_HOUR, 0);
		final TextView tvCurrentHour = (TextView) findViewById(R.id.tvCurrentHour);
		final SeekBar seekbar = (SeekBar) findViewById(R.id.seekBarHour);
		seekbar.setProgress(hour); // default 1 AM
		tvCurrentHour.setText(String.valueOf(hour));
		
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				sharedEdit.putInt(Constants.SHARE_KEY_NOTICE_HOUR, seekBar.getProgress());
				sharedEdit.commit();
				
				VttAlarmManager.replaceAlarm(mContext, seekBar.getProgress(), MyBroadcastReceiver.class);
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				// TODO Auto-generated method stub
				seekBar.setProgress(progress);
				tvCurrentHour.setText(String.valueOf(progress));
			}
		});
	}
}
